package io.lvdaxian.mybatis19.mapping;


import io.lvdaxian.mybatis19.session.Configuration;
import io.lvdaxian.mybatis19.type.JdbcType;
import io.lvdaxian.mybatis19.type.TypeHandler;
import io.lvdaxian.mybatis19.type.TypeHandlerRegistry;

public class ParameterMapping {
  
  private Configuration configuration;
  
  // property
  private String property;
  // javaType = int
  private Class<?> javaType = Object.class;
  // jdbcType=NUMERIC
  private JdbcType jdbcType;
  private TypeHandler<?> typeHandler;
  
  private ParameterMapping() {
  }
  
  public static class Builder {
    
    private final ParameterMapping parameterMapping = new ParameterMapping();
    
    public Builder(Configuration configuration, String property, Class<?> javaType) {
      parameterMapping.configuration = configuration;
      parameterMapping.property = property;
      parameterMapping.javaType = javaType;
    }
    
    public Builder javaType(Class<?> javaType) {
      parameterMapping.javaType = javaType;
      return this;
    }
    
    public Builder jdbcType(JdbcType jdbcType) {
      parameterMapping.jdbcType = jdbcType;
      return this;
    }
    
    public ParameterMapping build() {
      if (parameterMapping.typeHandler == null && parameterMapping.javaType != null) {
        Configuration configuration = parameterMapping.configuration;
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        parameterMapping.typeHandler = typeHandlerRegistry.getTypeHandler(parameterMapping.javaType, parameterMapping.jdbcType);
      }
      return parameterMapping;
    }
    
  }
  
  public Configuration getConfiguration() {
    return configuration;
  }
  
  public String getProperty() {
    return property;
  }
  
  public Class<?> getJavaType() {
    return javaType;
  }
  
  public JdbcType getJdbcType() {
    return jdbcType;
  }
  
  public TypeHandler<?> getTypeHandler() {
    return typeHandler;
  }
}
